package com.feisi.controller;

import com.feisi.domain.Contract;
import com.feisi.service.ContractService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/contract")
public class ContractController {

    @Autowired
    private ContractService contractService;

    /**
     * 查询所有合同
     *
     * @param request
     * @return
     */
    @RequestMapping("/findAll")
    public @ResponseBody
    Map<String, Object> findAll(HttpServletRequest request) {
        System.out.println("表现层:查询所有合同");
        Integer page = Integer.valueOf(request.getParameter("page"));
        Integer limit = Integer.valueOf(request.getParameter("limit"));
        List<Contract> contracts = contractService.findAll((page - 1) * limit, limit);
        int count = contractService.contractsTotal();
        System.out.println(contracts);
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("code", 0);
        map.put("msg", "");
        map.put("count", count);
        map.put("data", contracts);
        return map;
    }

    /**
     * 根据合同名称查询合同
     *
     * @param co_name
     * @param page
     * @param limit
     * @return
     */
    @RequestMapping("/findByName")
    public @ResponseBody
    Map<String, Object> findByName(String co_name, @RequestParam("page") Integer page, @RequestParam("limit") Integer limit) {
        List<Contract> contracts = contractService.findByName(co_name, (page - 1) * limit, limit);
        int count = contractService.contractsTotalByName(co_name);
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("code", 0);
        map.put("msg", "");
        map.put("count", count);
        map.put("data", contracts);
        return map;
    }


    /**
     * 新增合同
     *
     * @param co_name
     * @param co_number
     * @param co_custname
     * @param co_sign_time
     * @param co_money
     * @param co_start_time
     * @param co_end_time
     * @param co_cust_sign_name
     * @param co_us_name
     * @param co_creator
     * @param co_approver
     * @throws ParseException
     */
    @RequestMapping("/saveContract")
    public @ResponseBody
    Map<String, Object> saveContract(String co_name, String co_number, String co_custname, String co_sign_time, Float co_money,
                                     String co_start_time, String co_end_time, String co_cust_sign_name, String co_us_name,
                                     String co_creator, String co_approver) throws ParseException {

//        String co_create_time,创建时间new一个Date
//        String co_creator,创建人,从session中拿
//        String co_status合同审批状态,(已通过,未通过,待审批),新增时默认给待审批状态


        System.out.println("进入合同保存功能");
        System.out.println("co_name="+co_name+",co_money="+co_money+",co_creator="+co_creator);
        Contract contract = new Contract();
        contract.setCo_name(co_name);
        contract.setCo_number(co_number);
        contract.setCo_custname(co_custname);
        contract.setCo_money(co_money);
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        contract.setCo_sign_time(co_sign_time);
        contract.setCo_start_time(co_start_time);
        contract.setCo_end_time(co_end_time);
        contract.setCo_cust_sign_name(co_cust_sign_name);
        contract.setCo_us_name(co_us_name);
        contract.setCo_create_time(df.format(new Date()));
        contract.setCo_creator(co_creator);
        contract.setCo_approver(co_approver);
        contract.setCo_status("待审批");
        contractService.saveContract(contract);
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("code", 200);
        map.put("msg", "success");
        map.put("count", 0);
        map.put("data", null);
        return map;
    }

    /**
     * 根据id删除合同
     * 批量删除
     *
     * @param ids
     */
    @RequestMapping(value = "/deleteContract",method = {RequestMethod.POST})
    public @ResponseBody
    Map<String, Object> deleteContract(@RequestBody String[] ids) {
        Integer[] id = new Integer[ids.length];
        System.out.println("合同id的长度为："+ids.length);
        for (int i=0;i<ids.length;i++){
            System.out.println("要删除的合同的id有：id="+ids[i]);
            Integer idOne = Integer.valueOf(ids[i]);
            id[i]=idOne;

        }
        contractService.deleteContract(id);
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("code", 200);
        map.put("msg", "success");
        map.put("count", 0);
        map.put("data", null);
        return map;
    }


    /**
     * 改变合同状态(已通过,未通过,待审批)
     *
     * @param id     合同的id
     * @param status 合同的状态
     */
    @RequestMapping("/updateStatus")
    public @ResponseBody
    String updateStatus(Integer id, String status) {
        contractService.updateStatus(id, status);
        return "success";
    }


    @RequestMapping("/findByStatus")
    public @ResponseBody
    Map<String, Object> findByStatus(@RequestParam("co_status") String co_status, @RequestParam("page") Integer page, @RequestParam("limit") Integer limit) {

        List<Contract> contracts = contractService.findByStatus(co_status, (page - 1) * limit, limit);
        int count = contractService.contractsTotalByStatus(co_status);
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("code", 0);
        map.put("msg", "");
        map.put("count", count);
        map.put("data", contracts);
        return map;
    }
}
